<template>
    <div id="three-lines" class="canvas" :style="{width: '100%', height: canHeight + 'px'}"></div>
</template>
<script setup lang="ts">
import { defineProps, computed, onMounted} from 'vue'
import { createPinia } from 'pinia'
import * as echarts from 'echarts/core';
import {
    TitleComponent,
  type TitleComponentOption,
  ToolboxComponent,
  type ToolboxComponentOption,
  TooltipComponent,
  type TooltipComponentOption,
  GridComponent,
  type GridComponentOption,
  LegendComponent,
  type LegendComponentOption,
  MarkLineComponent,
  type MarkLineComponentOption,
  MarkPointComponent,
  type MarkPointComponentOption
} from 'echarts/components';
import { LineChart, type LineSeriesOption } from 'echarts/charts';
import { UniversalTransition } from 'echarts/features';
import { CanvasRenderer } from 'echarts/renderers';
import { useChartsStore } from '@/stores/charts';
const pinia = createPinia();
const chartStore = useChartsStore(pinia)

echarts.use([
  TitleComponent,
  ToolboxComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  LineChart,
  CanvasRenderer,
  UniversalTransition,
  MarkLineComponent,
  MarkPointComponent
]);

type EChartsOption = echarts.ComposeOption<
  | TitleComponentOption
  | ToolboxComponentOption
  | TooltipComponentOption
  | GridComponentOption
  | LegendComponentOption
  | LineSeriesOption
  | MarkLineComponentOption
  | MarkPointComponentOption
>;
const props = defineProps({
    canvasHeight: {
        type:Number,
        default: 300
    },
    data: {
        type: Object,
        default: () => { } 
    }
})

let canHeight = computed(() => { return props.canvasHeight })
onMounted(() => {
    let mPoint = {
        data: [{ 
            type: 'max', name: 'Max', symbol: 'circle',  symbolSize: 14,
            label: {
                position: 'start',
                formatter: (param:any) => { return '{a|增值：'+param.value+'k}'},
                rich: {
                    a: {
                        color: '#282828',
                        lineHeight: 15,
                        align: 'center',
                        padding: [10,20]
                    }
                }
            }         
        }],
    }

    var chartDom = document.getElementById('three-lines');
    var myChart = echarts.init(chartDom);
    var option: EChartsOption;
    
    option = {
        color: chartStore.colorsThree,
        title: {
            show: false,
            text: ''
        },
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            left: 0,
            align: 'left',
            data: props.data.seriesDataList.map(m => m.name)
        },
        grid: {
            top: 30,
            left: 0,
            right: '2%',
            bottom: 0,
            containLabel: true
        },
        toolbox: {
            show: false,
            feature: {
            saveAsImage: {}
            }
        },     
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: props.data.xAxisDataList,
            axisLabel: {
                show: true,
                color: '#c8c8c8',
                formatter: function (value, index) {
                    let month = value.indexOf('-') ? chartStore.monthToChi[parseInt(value.split('-')[1])] +'月' : value;
                    let year = value.indexOf('-') ? value.split('-')[0] : value;
                    
                    return [
                        '{a|'+month+'}',
                        index%6 === 0 ? '{a|'+year+'}' : ''
                    ].join('\n')
                },
                rich: {
                    a: {
                        color: '#c8c8c8',
                        lineHeight: 15,
                        align: 'left'
                    }
                }
            }
        },
        yAxis: {
            type: 'value',
            splitNumber: 3,
            axisLabel: {
                show: true,
                color: '#c8c8c8',
                formatter: function (value) { return value+'K' }
            }
        },
        series: props.data.seriesDataList.map(m => {
            m['markPoint'] = mPoint
            m['type'] = 'line'
            m['symbol'] = 'none'
            m['stack'] = 'Total'
            return m
        })
    };

    option && myChart.setOption(option);
})
</script>

<style scope>
</style>
